﻿using System;
using System.Windows.Forms;
using System.IO;

namespace OrganizeFilesInFolders
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSrcBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxSrc.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void buttonDstBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxDst.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void buttonDo_Click(object sender, EventArgs e)
        {
            string sourcePath = @textBoxSrc.Text;
            string targetPath = @textBoxDst.Text;

            if (System.IO.Directory.Exists(sourcePath))
            {
                if (System.IO.Directory.Exists(targetPath))
                {
                    //buttonDo.Text = "Cancel";
                    copyFiles(sourcePath, targetPath);
                }
                else
                {
                    MessageBox.Show("Destination Folder " + targetPath + " does not exists", "Error");
                }
            }
            else
            {
                MessageBox.Show("Source Folder " + sourcePath + " does not exists", "Error");
            }


            
            
        }
        
        private void copyFiles(string sourcePath, string targetPath)
        {
            //Set some strings
            string fileName;
            string destFile;
            string srcFileFullPath;
            string destFolder;
            string fileYear;
            string fileMonth;
            string fileDay;
            string fileDate;


            //Init progress bar
            pBar1.Visible = true;
            pBar1.Minimum = 1;
            pBar1.Value = 1;
            pBar1.Step = 1;

            //Get all files in the folder
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Set Maximum to the total number of files to copy.
            pBar1.Maximum = files.Length;

            // Copy the files
            foreach (string s in files)
            {
                // Get Filename
                fileName = System.IO.Path.GetFileName(s);

                // Create the full path
                srcFileFullPath = System.IO.Path.Combine(sourcePath, fileName);

                //Get the creation date of the file and set it in folder name format
                DateTime creationTime = File.GetCreationTime(srcFileFullPath);
                fileYear = creationTime.Year.ToString("D4");
                fileMonth = creationTime.Month.ToString("D2");
                fileDay = creationTime.Day.ToString("D2");
                fileDate = fileYear + "-" + fileMonth + "-" + fileDay;

                //Set destination folder name
                destFolder = System.IO.Path.Combine(targetPath, fileDate);

                // Try to create the destination date folder if it's missing
                try
                {
                    if (!System.IO.Directory.Exists(destFolder))
                    {
                        System.IO.Directory.CreateDirectory(destFolder);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                // Create destination full path
                destFile = System.IO.Path.Combine(destFolder, fileName);

                //Copy the file to the destination folder
                try
                {
                    //Copy the file
                    System.IO.File.Copy(s, destFile, true);
                    //Update the progress bar
                    pBar1.PerformStep();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
